package services;
import java.io.*;
import java.net.*;

import pd.SwissSMSClient;

public class Hispeed implements SMSService {
	
	private static final String host = "https://your.hispeed.ch";
	private String authcookie = "";
	
	public boolean doLogin(String userid, String password){
		String postheader = 
			"POST /setcookie.cgi HTTP/1.1\r\n" +
			"Host: your.hispeed.ch\r\n" +
			"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
			"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
			"Accept-Encoding: deflate\r\n" +
			"Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n" +
			"Keep-Alive: 300\r\n" +
			"Connection: close\r\n" +
			"Referer: https://your.hispeed.ch/yourauth.cgi?https://your.hispeed.ch/\r\n" +
			"Content-Type: application/x-www-form-urlencoded\r\n" +
			"Content-Length: ";
		
		String postbody = "";
		try {
			postbody = "url=http%3A%2F%2Fyour.hispeed.ch%2F&mail=" + URLEncoder.encode(userid, "UTF8") + "&password=" + URLEncoder.encode(password, "UTF8") + "&iscrizione.x=0&iscrizione.y=0";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
		
		String post = postheader + postbody.length() + "\r\n\r\n" + postbody;
		
		String response = SwissSMSClient.sendSSL(host, post);
		
		String strsetcookie = "Set-Cookie: TornadoAuth=";
		int coopos = response.indexOf(strsetcookie);
		int delpos = response.indexOf("\r\n\r\n");
		
		if(coopos == -1 || delpos == -1 || coopos > delpos){
			return false;
		}
		
		authcookie = response.substring(coopos + strsetcookie.length(), delpos);
		authcookie = authcookie.substring(0, authcookie.indexOf(';'));
		
		String getrequest = 
			"GET / HTTP/1.1\r\n" +
			"Host: your.hispeed.ch\r\n" +
			"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
			"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
			"Accept-Encoding: deflate\r\n" +
			"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
			"Keep-Alive: 300\r\n" +
			"Connection: close\r\n" +
			"Referer: https://your.hispeed.ch/yourauth.cgi?https://your.hispeed.ch/\r\n" +
			"Cookie: TornadoAuth=" +
			authcookie +
			"\r\n\r\n";

		response = SwissSMSClient.sendSSL(host, getrequest);

		if(response.indexOf("Location: de/apps/webmail/") != -1){
			return true;
		}
		
		return false;
	}
	
	public boolean sendMessage(String number, String message){
		
		String postheaderp1 = 
			"POST /youth/sms_senden-de.aspx HTTP/1.1\r\n" +
			"Host: www.swisscom-mobile.ch\r\n" +
			"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
			"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
			"Accept-Encoding: deflate\r\n" +
			"Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n" +
			"Keep-Alive: 300\r\n" +
			"Connection: close\r\n" +
			"Referer: https://www.swisscom-mobile.ch/youth/sms_senden-de.aspx\r\n" +
			"Cookie: Navajo=";
		
		String postheaderp2 = "; CP=null*; TreeView=00000000; Filename=../youth/sms_senden-de.aspx\r\n" +
			"Content-Type: application/x-www-form-urlencoded\r\n" +
			"Content-Length: ";
		
		String postbodyp1 = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE_SCM=2&__VIEWSTATE=&CobYouthSMSSenden%3AtxtMessage=";
		String postbodyp2 = "&CobYouthSMSSenden%3AtxtNewReceiver=";
		String postbodyp3 = "&CobYouthSMSSenden%3AbtnSend=Senden&FooterControl%3AhidNavigationName=SMS+senden&FooterControl%3AhidMailToFriendUrl=youth%2Fsms_senden-de.aspx";
		String postbody;
		try {
			postbody = postbodyp1 + URLEncoder.encode(message, "UTF8") + postbodyp2 + URLEncoder.encode(number, "UTF8") + postbodyp3;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
		
		String post = postheaderp1 + authcookie + postheaderp2 + postbody.length() + "\r\n\r\n" + postbody;
		
		SwissSMSClient.sendSSL(host, post);	
		return true;
	}

}
